//
//  LoanMainView.m
//  FinancialStore
//
//  Created by zhenqiu on 16/8/4.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoanMainView.h"


@implementation LoanMainView
@synthesize processView;
@synthesize inputTableView;
@synthesize nextBtn;


-(instancetype)init{
    
    if(self = [super init]){
        
        [self setFrame:[[UIScreen mainScreen] bounds]];
        self.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
        [self initProcessView];
        [self initTableView];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification  object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification  object:nil];
        
    }
    
    [self updateConstraintsIfNeeded];
    return self;
}



-(void)updateConstraints{
    [super updateConstraints];

    [processView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self);
        make.height.mas_equalTo(LOAN_PROCESS_VIEW_HEIGHT);
        
    }];
    
    [inputTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(processView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom).offset(0); //leon changed
        
    }];
    
    
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark -
#pragma mark init subview 

-(void)initProcessView{
    
    processView = [[LoanProcessView alloc] init];
    [self addSubview:processView];
    
}

-(void)initTableView{
    inputTableView = [[UITableView alloc] init];
    [self addSubview:inputTableView];
}



#pragma mark -
#pragma mark update subview 



#pragma mark -
#pragma mark view api 

-(id)genSectionHeaderView{
    
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
    
    UIView * bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithString:UI_COLOR_SPLIT_LINE_1];
    [view addSubview:bottomLine];
    
//    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view.mas_left);
//        make.right.equalTo(view.mas_right);
//        make.height.mas_equalTo(0.5);
//        make.bottom.equalTo(view.mas_bottom).with.offset(-0.5);
//        
//    }];
    
    return view;
    
    
}

-(void)setTableViewFooter{
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, NEXT_BTN_TOP + NEXT_BTN_HEIGHT + NEXT_BTN_BOTTOM)];
    bgView.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
    nextBtn = [[UIButton alloc] init];
    nextBtn.layer.cornerRadius = VIEW_RADIUS;
    [nextBtn setBackgroundColor:[UIColor colorWithString:UI_COLOR_MAIN_BG_3]];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:nextBtn];
    
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(bgView.mas_left).with.offset(LEFT_RIGHT_SPACE);
        make.right.equalTo(bgView.mas_right).with.offset(-LEFT_RIGHT_SPACE);
        make.top.equalTo(bgView.mas_top).with.offset(NEXT_BTN_TOP);
        make.height.mas_equalTo(NEXT_BTN_HEIGHT);
        
    }];
    
    self.inputTableView.tableFooterView = bgView;
}



#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    inputTableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
    inputTableView.contentInset = UIEdgeInsetsZero;
}



#pragma mark -
#pragma mark touchbegin 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}



@end
